package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.StringUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.TagTextView;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：新发地  分类  产品
 */
public class XFDProductAdapter extends RecyclerView.Adapter<XFDProductAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<HomeSearchXFDGoodBeen.DataBean> mList;
    private Context mContext;


    public XFDProductAdapter(Context context,List<HomeSearchXFDGoodBeen.DataBean> list) {
        this.mList = list;
        this.mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_xfd_product, parent, false);
        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        holder.ivProduct.setImageResource(R.drawable.image_product);
        RoundedCorners roundedCorners = new RoundedCorners(20);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback( R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
        Glide.with(mContext).load(mList.get(position).getThumb()).apply(options.transform(new CenterCrop(), roundedCorners)).into( holder.ivProduct);
        holder.ttvProduct.setSingleTagAndContent("新发地", mList.get(position).getTitle(),R.drawable.d02c557_bg_r3,"#FFFFFFFF");
        holder.tv_maozhong.setText("毛重: " + mList.get(position).getGoods_weight() + "斤");
        if (mList.get(position).getUnit().isEmpty()){
            if (Double.parseDouble(mList.get(position).getDiscountprice())==0){
                holder.tvSpecifications.setText("¥"+mList.get(position).getMarketprice());
                holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getMarketprice(),mList.get(position).getGoods_weight())+ "/斤");

            }else {
                holder.tvSpecifications.setText("¥"+mList.get(position).getDiscountprice());
                holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getDiscountprice(),mList.get(position).getGoods_weight())+ "/斤");

            }
        }else {
            if (Double.parseDouble(mList.get(position).getDiscountprice())==0){
                holder.tvSpecifications.setText("¥"+mList.get(position).getMarketprice()+"/"+mList.get(position).getUnit());
                holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getMarketprice(),mList.get(position).getGoods_weight())+ "/斤");

            }else {
                holder.tvSpecifications.setText("¥"+mList.get(position).getDiscountprice()+"/"+mList.get(position).getUnit());
                holder.tv_aboutweight.setText("约¥" + StringUtil.initAboutPrice(mList.get(position).getDiscountprice(),mList.get(position).getGoods_weight())+ "/斤");

            }

        }
        holder.tvClinchDealPrice.setText("销量 "+mList.get(position).getSale_num());
        if (null==mList.get(position).getStore_name()){
            holder.tvCompany.setText("本农产品由新发地直接出售");
        }else {
            if (mList.get(position).getStore_name().isEmpty()){
                holder.tvCompany.setText("本农产品由新发地直接出售");
            }else {
                holder.tvCompany.setText(mList.get(position).getStore_name());
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Utils.isFastClick()) {
                    mItemClickListener.onItemClick(position);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return mList ==null?0: mList.size();
    }

    public void setData(List<HomeSearchXFDGoodBeen.DataBean> data) {
        this.mList =data;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.iv_product)
        ImageView ivProduct;
        @BindView(R.id.ttv_product)
        TagTextView ttvProduct;
        @BindView(R.id.tv_company)
        TextView tvCompany;
        @BindView(R.id.tv_specifications)
        TextView tvSpecifications;
        @BindView(R.id.tv_clinch_deal_price)
        TextView tvClinchDealPrice;
        @BindView(R.id.tv_aboutweight)
        TextView tv_aboutweight;
        @BindView(R.id.tv_maozhong)
        TextView tv_maozhong;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
